
#ifndef TOKEN_RING_FORMATS_H
#define	TOKEN_RING_FORMATS_H

#include <vector>
#include <systemc>

class packet
{
public:    
           
    //
    // Определение формата полей в пакетах
    //

    //
    // Starting delimeter field - |J|K|0|J|K|0|0|0|
    // J and K - non data symbol (Differential Manchester encoding)
    // Token, frame or abort sequence always start with a starting delimiter
    //  
    typedef unsigned char starting_delimiter;
    static const starting_delimiter SD = 0x90;      // b10010000, J-1, K-0

    //
    // Ending delimeter field - |J|K|1|J|K|1|I|E|
    // J and K - non data symbol (Differential Manchester encoding)
    // I - intermediate frame bit
    // E - error detection bit
    //
    typedef unsigned char ending_delimiter;
    static const starting_delimiter ED_BASE = 0x90;  // b10110100, J-1, K-0
    static const unsigned char ED_E_MASK = 0x01;     // error detection bit mask
    static const unsigned char ED_I_MASK = 0x02;     // intermediate frame bit mask

    //
    // Access control field - |P|P|P|T|M|R|R|R| 
    // P - priority bits
    // T - token bit, 0 - token, 1 - frame
    // M - monitor bits, prevent continuous circulating token or frame on the ring
    // R - reservation bits
    //
    typedef struct access_control
    {
        unsigned char ppp   : 3;
        unsigned char t     : 1;
        unsigned char m     : 1;
        unsigned char rrr   : 3;
        
        bool operator==(const access_control& rhs);
                        
    } access_control;
    
    static const unsigned char AC_T_MASK = 0x10;
    static const unsigned char AC_M_MASK = 0x08;

    //
    // Frame control field - |F|F|Z|Z|Z|Z|Z|Z|
    // F - frame type bites 
    // Z - frame control bites
    //
    typedef unsigned char frame_control;

    //
    // MAC-address 
    //
    static const int m_BYTE_ADDR_SIZE = 6;
    static const unsigned long long m_ADDR_MASK = 0x0000FFFFFFFFFFFF;
    typedef unsigned long long mac_address;
    typedef mac_address source_address;
    typedef mac_address destination_address;
    

    static const mac_address m_MONITOR_ARRD;

    //
    // Routing information
    //
    typedef std::vector<unsigned char> routing_info;
    static const std::size_t m_MAX_RI_SIZE = 30;

    //
    // Information field
    //
    typedef std::vector<unsigned char> data_field;
    static std::size_t m_MAX_DATA_SIZE;

    //
    // Frame-check sequence field (FCS) 
    //
    typedef unsigned long frame_check_seq;
    
    //
    // Frame status field - |A|C|r|r|A|C|r|r|
    // A - address recognized bit
    // C - frame copied bit
    // 
    typedef unsigned char frame_status;
    static const unsigned char m_FS_A_MASK = 0x88;
    static const unsigned char m_FS_C_MASK = 0x66;
    
    
    virtual ~packet();
    virtual packet *clone();
    
    bool operator==(const packet& rhs);    
    
protected:            
    
    virtual bool equals_packet(const packet& pkt);        
};

//
//
//
void sc_trace(sc_core::sc_trace_file& tf, const packet& p,
               const std::string& name);

void sc_trace(sc_core::sc_trace_file *tf, const packet& p, 
               const std::string& name);

//
//
//
std::ostream& operator<<(std::ostream& os, const packet& p);


#endif

